package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/11/19 15:50
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Reference
    MenuService menuService;

    // 从SpringSecurity中获取用户信息
    @RequestMapping(value = "/getUsername")
    public Result getUsername() {
        try {
            //从springsecurity中获取用户信息
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            //根据用户名查询用户信息,根据用户ID关联角色表查询出该用户的权限
            List<Map<String,Object>> menuList = menuService.ShowMenuByUserRole(user.getUsername());

            //创建需要返回的Map集合
            Map<String,Object> params = new HashMap<>();
            params.put("username",user.getUsername());
            params.put("menuList",menuList);

            return new Result(true, MessageConstant.SHOW_MENU_LIST_SUCCESS,params);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SHOW_MENU_LIST_FAIL);
        }
    }

}
